package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzdy.dataplatform.controller.api.DelArr;
import com.fzdy.dataplatform.controller.api.ModuleCallback;
import com.fzdy.dataplatform.controller.api.app.AppEditApi;
import com.fzdy.dataplatform.controller.api.app.AppListApi;
import com.fzdy.dataplatform.controller.api.sysLog.LogStringUtil;
import com.fzdy.dataplatform.controller.api.sysLog.SysLogApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.Check;
import com.fzdy.dataplatform.core.controller.BaseController;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.entity.App;
import com.fzdy.dataplatform.entity.User;
import com.fzdy.dataplatform.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/sysmanage/app")
public class AppController extends BaseController {

    @Autowired
    private AppService appService;


    @RequestMapping(value = "/list")
    public String list(@RequestBody Page<App> page, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        App app = new App();
        app.setAppname(page.getName());
        if (!webUtilify.isAdmin(request)) {
            app.setUserid(webUtilify.LoginUserinfo(request).getId());
        } else {
            if (!Check.checkStringNull(page.getUname())) {
                User user = new User(page.getUname());
                app.setUser(user);
            }
        }
        app.setStatus(1);
        appService.getPageByExampleLike(app, page);
        ajaxCallback = new AjaxCallback(ktvGroupdbtoapi(page.getList(), webUtilify.isAdmin(request)), page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);
    }

    private List ktvGroupdbtoapi(List<App> list, Boolean isadmin) {
        List<AppListApi> apis = new ArrayList<>();
        for (App app : list) {
            AppListApi api = new AppListApi(app.getId(), app.getPushaddress(), app.getToken(), app.getUser().getName(), app.getAppname());
            api.setUserid(app.getUserid());
            if (!isadmin) {
                api.setUname(null);
            }
            apis.add(api);
        }
        return apis;
    }

    @RequestMapping(value = "/getApp")
    public String getApp(@RequestBody App app, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        User user = webUtilify.LoginUserinfo(request);
        app = appService.get(app.getId());
        if (app == null) {
            ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "找不到对应的应用");
            return JSON.toJSONString(ajaxCallback);
        }
        if (!webUtilify.isAdmin(request) && !user.getId().equals(app.getUserid())) {
            ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "找不到对应的应用");
            return JSON.toJSONString(ajaxCallback);
        }
        AppListApi api = new AppListApi(app.getId(), app.getPushaddress(), app.getToken(), app.getUser().getName(),
                app.getAppname());
        api.setAppmemo(app.getMemo());

        ajaxCallback = new AjaxCallback();
        ajaxCallback.setData(api);
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/edit")
    public String edit(@RequestBody AppEditApi api, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        User user = webUtilify.LoginUserinfo(request);
        /** 日志*/
        SysLogApi log_sysLogApi = new SysLogApi(LogStringUtil.APP, LogStringUtil.SUCCESS, user.getId());
        JSONObject log_content = new JSONObject();
        ajaxCallback = new AjaxCallback();
        if (api.getId() != null) {
            log_sysLogApi.setOperation(LogStringUtil.UPDATA);
            App app = appService.get(api.getId());
            log_content.put(LogStringUtil.BEFOREEDIT,  app.toString());
            app.GeneralParams(api.getAppname(), api.getUrl(), api.getToken(), api.getAppmemo());
            log_content.put(LogStringUtil.AFTEREDIT,  app.toString());
            appService.updateT(app);
            ajaxCallback.setMessage("修改成功");
        } else {
            log_sysLogApi.setOperation(LogStringUtil.ADD);
            App app = new App(api.getAppname(), api.getUrl(), user.getId(), api.getToken(), api.getAppmemo());
            log_content.put("应用信息", app.toString());
            appService.insertT(app);
            ajaxCallback.setMessage("添加成功");
        }
        log_sysLogApi.setContent(log_content);
        syslogService.WriteLog(log_sysLogApi);

        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody DelArr arr, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        JSONObject log_content = new JSONObject();
        log_content.put("id", JSON.toJSONString(arr.getIds()));
        appService.batchDelete(arr.getIds());
        syslogService.WriteLog(new SysLogApi(LogStringUtil.DEL, LogStringUtil.APP, LogStringUtil.SUCCESS,
                log_content, webUtilify.LoginUserinfo(request).getId()));
        ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "删除成功");
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/testAddress")
    public String testAddress(@RequestBody AppListApi api, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
//        JSONObject re = HttpTool.sendReqMsg(JSON.toJSONString(api), api.getUrl());
//        if (re == null) {
//            ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "网址不能使用");
//            return JSON.toJSONString(ajaxCallback);
//        }
//        System.out.println(re);
        ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "连接成功");
        return JSON.toJSONString(ajaxCallback);
    }
}
